const jwt = require('jsonwebtoken')
const selfKey = "!@#%^&*$$1324678%@#$^"

// iss：Issuer，发行者
// sub：Subject，主题
// aud：Audience，观众
// exp：Expiration time，过期时间
// nbf：Not before
// iat：Issued at，发行时间
// jti：JWT ID
module.exports = class extends think.Service {
  
  async sign(user) {
    const author = {
      "iss": "admin",
    }
    return new Promise((reslove, reject) => {
      jwt.sign({...user, ...author}, selfKey, { expiresIn: '2h' }, (err, token) => {
        if(err) {
          reject(err)
          return
        }
        reslove(token)
      })
    })
  }

  async verify(token) {
    try{
      const decoded = jwt.verify(token, selfKey)
      const res = decoded.exp * 1000 > Date.now() ? decoded : null
      return res
    } catch(err) {
      think.logger.info(err)
      return err
    }
  }
};
       